import { getAppInfo } from '@/api/user'
import { configUrl } from '@/config'
const appUpDate = {
  state: {
  },
  mutations: {
  },
  actions: {
    AndroidCheckUpdate({ commit }, info) {
      plus.nativeUI.confirm('有新的版本发布，新版本下载完成后将自动弹出安装程序。', (e) => {
        const dtask = plus.downloader.createDownload(info, {}, (d, status) => {
          //下载完成  
          if (status == 200) {
            plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, {}, (error) => {
              plus.nativeUI.toast('安装失败')
            })
          } else {
            plus.nativeUI.toast('更新失败')
          }
        })
        try {
          dtask.start()
          var prg = 0;
          var showLoading = plus.nativeUI.showWaiting("正在下载")
          dtask.addEventListener('statechanged', (task, status) => {
            switch (task.state) {
              case 1:
                showLoading.setTitle("正在下载");
                break;
              case 2:
                showLoading.setTitle("已连接到服务器");
                break;
              case 3:
                prg = parseInt(
                  (parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100
                )
                showLoading.setTitle("  正在下载" + prg + "%  ");
                break;
              case 4:
                plus.nativeUI.closeWaiting();
                //下载完成
                break;
            }
          });
        } catch (err) {
          plus.nativeUI.closeWaiting();
          plus.nativeUI.toast('更新失败')
        }
      }, {
        "title":"版本更新",
        "buttons":["确定"],
        "verticalAlign":"bottom"
      })
    },
    IosCheckUpdate({ commit }, info) {
      plus.nativeUI.confirm('有新的版本发布，请更新版本', (e) => {
        plus.runtime.openURL(info)
      }, {
        "title":"版本更新",
        "buttons":["确定"],
        "verticalAlign":"bottom"
      })
    },
    // 获取版本信息
    getAppInfoFun({ commit }, info) {
      return new Promise((resolve, reject) => {
        getAppInfo(info).then(data => {
          resolve(data)
        }).catch(e => {
          resolve(e)
        })
      })
    },
    // 检查更新
    async CheckAppUpdate({ commit, dispatch }, info) {
      uni.getSystemInfo({
        complete: async (res) => {
          // 配置了访问地址才访问
          if (configUrl.baseUrl.indexOf('http:') > -1 || configUrl.baseUrl.indexOf('https:') > -1) {
            const appInfo = await dispatch("getAppInfoFun", { appType: res.platform })
            if (appInfo && appInfo.code === 0 && appInfo.data) {
              if (res.appVersion !== appInfo.data.version) {
                //检测当前平台，如果是安卓则启动安卓更新
                if (res.platform == "android") {
                  dispatch("AndroidCheckUpdate", appInfo.data.downloadUrl)
                }
                // IOS跳转到appstore
                if (res.platform == 'ios') {
                  dispatch("IosCheckUpdate", appInfo.data.downloadUrl)
                }
              }
            }
          }
        }
      })
    }
  }
}

export default appUpDate